#!/usr/local/bin/ruby
=begin

Copyright 2010 Arachne Jericho <arachne.jericho@gmail.com>

This file is part of RubyTea.

RubyTea is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RubyTea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RubyTea.  If not, see <http://www.gnu.org/licenses/>.

=end
# 
# Lists teas reviewed from the tea collection in a nice way.
#

require 'ruby-tea.rb'
require 'cgi'

header('Rated Teas')
puts <<END
<p>
Note: ratings may change for teas over time, relative to the 
rest of the collection. Reviews never contain star ratings for 
this reason.
</p>

<table style="width: 99.9%;">
END

# Returns a flattened list of teas from the collection that having 
# the given rating.
def get_teas_for_rating(rating)
    teas = []
    $tea_collection.keys.sort.each do |blender|
        $tea_collection[blender].keys.sort.each do |tea_name|
            tea = $tea_collection[blender][tea_name]

            next if (!(tea.has_key? 'Rating') || (tea['Rating'].to_i != rating))

            tea['Blender'] = blender
            tea['Name'] = tea_name
            teas << tea
        end
    end
    teas
end

[5, 4, 3, 2, 1, 0].each do |rating|

    puts <<-END
    <tr><th colspan="3">#{rating} Stars</th></tr>
    END

    (get_teas_for_rating rating).each do |tea|
        if tea.has_key? 'Review'
            review_html = %Q(<a target="_top" href="#{CGI::escapeHTML tea['Review']}" title="Review for #{CGI::escapeHTML tea['Name']}">Review</a></tr>)
        else
            review_html = ''
        end

        puts %Q(<tr class="#{CGI::escapeHTML tea['Type']}"><td>#{CGI::escapeHTML tea['Blender']}</td><td>#{CGI::escapeHTML tea['Name']}</td><td>#{tea['Rating']} #{review_html}</td></tr>)
    end
end

puts '</table>'
footer()
